const upload = require('../util/upload')
const Sequelize = require('sequelize')
const models = require('../models')


exports.upload = async (file, name) => {
  let fileObj = await upload.upload(file)
  await models.files.create({...fileObj, ...{name: name}})
}

/**
 * 文件列表
 * @param start
 * @param limit
 * @returns {Promise<*>}
 */
exports.findFiles = async (start, limit) => {
  let sql = 'SELECT * FROM files LIMIT :start,:end'
  let result = models.sequelize.query(sql, {
    replacements: {
      start: (start - 1) * limit,
      end: start * limit
    },
    type: 'SELECT'
  })
  return result
}